﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Constructor
{
    public partial class ExemplarSlot : UserControl
    {
        private Slot _slot;
        public ExemplarSlot(Slot slot)
        {
            InitializeComponent();
            _slot = slot;
            Init();
        }

        private void Init()
        {
            lbl_name.Text = _slot.Name;
            switch (_slot.Type)
            {
                case CommonLib.SlotType.Regular:
                    if (_slot.Domain != null)
                    {
                        switch (_slot.Domain.Type)
                        {
                            case CommonLib.DomainType.Enum:
                                InitEnum();
                                break;
                            case CommonLib.DomainType.Image:
                                InitImage();
                                break;
                            case CommonLib.DomainType.Number:
                                InitNumber();
                                break;
                        }
                        ChangeVisible(_slot.Domain.Type);
                    }
                    break;
                case CommonLib.SlotType.SubFrame:
                    InitSubFrame();
                    break;
            }
        }

        private void InitSubFrame()
        {
            cmb_value.Items.Clear();
            cmb_value.SelectedIndexChanged += new EventHandler(cmb_value_subframe_SelectedIndexChanged);
            ChangeVisible(CommonLib.DomainType.Enum);
            if (_slot.Subframe == null) return;
            string subframe = _slot.Subframe.Name;
            if (External.Parents.ContainsKey(_slot.Subframe.ID))
                subframe = External.Parents[_slot.Subframe.ID].Name;
            List<Frame> frames = MLO.MLOSource.Exemplars;
            foreach (Frame f in frames)
                if (External.Parents.ContainsKey(f.ID) && External.Parents[f.ID].Name == subframe)
                    cmb_value.Items.Add(f.Name);
            if (_slot.Subframe.Type == CommonLib.FrameType.Exemplar)
                cmb_value.Text = _slot.Subframe.Name;
        }

        private void InitNumber()
        {
            num_value.ValueChanged -= num_value_ValueChanged;
            num_value.Minimum = _slot.Domain.MinValue;
            num_value.Maximum = _slot.Domain.MaxValue;
            int temp = _slot.Domain.MinValue;
            int.TryParse(_slot.Default.ToString(), out temp);
            if (temp >= _slot.Domain.MinValue && temp <= _slot.Domain.MaxValue)
                num_value.Value = temp;
            num_value.ValueChanged += num_value_ValueChanged;
        }
        private void InitImage()
        {
            if (External.images.LoadedImages != null && _slot.Default != null && External.images.LoadedImages.ContainsKey(_slot.Default.ToString()))
                image.BackgroundImage = External.images.LoadedImages[_slot.Default.ToString()].BackgroundImage;
        }
        private void InitEnum()
        {
            cmb_value.Items.Clear();
            cmb_value.SelectedIndexChanged += new EventHandler(cmb_value_SelectedIndexChanged);
            foreach (string value in _slot.Domain.Values)
                cmb_value.Items.Add(value);
            if (_slot.Default != null)
                cmb_value.Text = _slot.Default.ToString();
            else
                if (cmb_value.Items.Count > 0) cmb_value.SelectedIndex = 0;
        }
        private void ChangeVisible(CommonLib.DomainType type)
        {
            switch (type)
            {
                case CommonLib.DomainType.Enum:
                    num_value.Visible = false;
                    cmb_value.Visible = true;
                    image.Visible = false;
                    break;
                case CommonLib.DomainType.Image:
                    num_value.Visible = false;
                    cmb_value.Visible = false;
                    image.Visible = true;
                    break;
                case CommonLib.DomainType.Number:
                    num_value.Visible = true;
                    cmb_value.Visible = false;
                    image.Visible = false;
                    break;
            }
        }
        public void KoreanRandom()
        {
            if (_slot.Type == CommonLib.SlotType.Regular)
            {
                Domain d = _slot.Domain;
                switch (d.Type)
                {
                    case CommonLib.DomainType.Enum:
                        Random rand = new Random(DateTime.Now.Millisecond);
                        if (d.Values.Count > 0)                        
                            cmb_value.SelectedIndex =  rand.Next(0, d.Values.Count - 1);                        
                        break;
                    case CommonLib.DomainType.Number:
                        rand = new Random(DateTime.Now.Millisecond);
                        num_value.Value = rand.Next(d.MinValue, d.MaxValue);
                        break;
                }
            }

        }
        private void image_Click(object sender, EventArgs e)
        {
            ImagePicker picker = new ImagePicker(sender as PictureBox);
            picker.Location = MousePosition;
            picker.ShowDialog();
        }

        private void image_BackgroundImageChanged(object sender, EventArgs e)
        {
            _slot.Default = image.BackgroundImage.Tag.ToString();
            _slot.Frame.RepaintFieldItem();
        }

        private void num_value_ValueChanged(object sender, EventArgs e)
        {
            _slot.Default = num_value.Value;
        }

        private void cmb_value_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmb_value.SelectedItem != null)
                _slot.Default = cmb_value.Text;
        }
        private void cmb_value_subframe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmb_value.SelectedItem != null)
            {
                _slot.Subframe = External.SearchFrameIn(MLO.MLOSource.Exemplars, cmb_value.Text);
                if (External.Parents.ContainsKey(_slot.Subframe.ID) && External.Parents[_slot.Subframe.ID].Name == "Клетка" && _slot.Name == "Позиция")
                    _slot.Frame.FieldItem.Move(_slot.Subframe);
            }
        }
    }
}
